package ar.uba.fi.tonyvaliente;

import java.util.LinkedList;

import ar.uba.fi.tonyvaliente.commands.AddDocumentCommand;
import ar.uba.fi.tonyvaliente.commands.AddDocumentsCommand;
import ar.uba.fi.tonyvaliente.commands.CleanCommand;
import ar.uba.fi.tonyvaliente.commands.Command;
import ar.uba.fi.tonyvaliente.commands.CommandResult;
import ar.uba.fi.tonyvaliente.commands.CreateIndexCommand;
import ar.uba.fi.tonyvaliente.commands.CreateSignatureCommand;
import ar.uba.fi.tonyvaliente.commands.CreateSignaturesCommand;
import ar.uba.fi.tonyvaliente.commands.CreditsCommand;
import ar.uba.fi.tonyvaliente.commands.DumpCommand;
import ar.uba.fi.tonyvaliente.commands.InitializeSignatureCommand;
import ar.uba.fi.tonyvaliente.commands.QueryDocumentsCommand;
import ar.uba.fi.tonyvaliente.commands.QuerySignatureCommand;
import ar.uba.fi.tonyvaliente.commands.ShowDocumentCommand;

public class Main {

	public static void main(String[] args) {
		showProgramInfo();

		LinkedList<Command> commands = new LinkedList<Command>();

		commands.add(new CleanCommand(args));
		
		commands.add(new InitializeSignatureCommand(args));
		commands.add(new CreateSignatureCommand(args));
		commands.add(new QuerySignatureCommand(args));
		commands.add(new CreateSignaturesCommand(args));
		
		commands.add(new CreateIndexCommand(args));
		commands.add(new AddDocumentsCommand(args));
		commands.add(new AddDocumentCommand(args));
		commands.add(new QueryDocumentsCommand(args));
		commands.add(new ShowDocumentCommand(args));
		commands.add(new DumpCommand(args));
		commands.add(new CreditsCommand(args));

		boolean recognized = false;

		for (Command command : commands) {
			CommandResult result = command.execute();
			if (!result.isRecognized())
				continue;
			recognized = true;

			println();
			println(result.getMessage());
			println();
			break;
		}

		if (!recognized)
			showUsage(commands);
	}

	private static void showProgramInfo() {
		println("Ultra Mini Basic Text Search Engine - Tony Valiente (c) 2008");
	}

	private static void showUsage(LinkedList<Command> commands) {
		String mainClassName = getMainClassName();

		println();
		printf_("Usage: java %s -command arguments", mainClassName);
		println();
		println();

		for (Command command : commands) {
			if (command.isShadow())
				continue;

			println("     " + command.getSignature());
			println();
			println("       " + command.getDescription());

			for (int i = 0; i < command.getExamples().length; i++) {
				if (i == 0)
					printf_("       Example: ");
				else
					printf_("                ");
				printf_("java %s " + command.getExamples()[i], mainClassName);
				println();
			}
			println();
			println();
		}
		println();
		println();
	}

	private static void println(String message) {
		System.out.println(message);
	}

	private static void println() {
		System.out.println("");
	}

	private static void printf_(String format, Object... args) {
		System.out.printf(format, args);
	}

	private static String getMainClassName() {
		Exception e = new Exception();
		StackTraceElement[] stackTrace = e.getStackTrace();
		return stackTrace[stackTrace.length - 1].getClassName();
	}
}
